package com.zkhz.base.security.util;

import com.zkhz.base.common.service.CurrentUserInfoService;
import com.zkhz.base.security.service.DefaultCurrentUserInfoServiceImpl;
import com.zkhz.base.security.token.TokenValueSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * @Description 当前用户工具类
 *
 * @author wuzeqin
 * @date 2020/5/18
 **/
@Component
public class CurrentUserUtil {

    private static CurrentUserInfoService UserInfoService;

    public CurrentUserUtil(@Autowired(required = false) CurrentUserInfoService userInfoService) {
        CurrentUserUtil.UserInfoService = userInfoService != null ? userInfoService : new DefaultCurrentUserInfoServiceImpl();
    }

    /**
     * @return 用户主键
     */
    public static String getUserId() {
        return UserInfoService.getProperty("id");
    }

    /**
     * @return 账号
     */
    public static String getUsername() {
        return UserInfoService.getProperty("username");
    }

    /**
     * @return 获取用户对应租户的ID
     */
    public static String getTenantId() {
        return UserInfoService.getProperty("tenantId");
    }

    /**
     * @return 当前用户是否为租户管理员
     */
    public static Boolean isTenantAdmin() {
        return UserInfoService.getProperty("isAdmin", Boolean.class, Boolean.FALSE);
    }

    /**
     * @return 判断当前用户是否为超级管理员
     */
    public static boolean isSuperAdmin() {
        return isSuperAdmin(getUserId());
    }

    /**
     * @return 判断用户是否为超级管理员
     */
    public static boolean isSuperAdmin(String userId) {
        return "admin".equals(userId) || "system".equals(userId);
    }

    /**
     * 根据属性名称获取对应值
     *
     * @param propertyName 属性名称
     * @return
     */
    public static String getProperty(String propertyName) {
        return UserInfoService.getProperty(propertyName);
    }

    /**
     * 根据属性名称获取对应值，需要指定值的类型
     *
     * @param propertyName 属性名称
     * @param valueType    值的类型
     * @return
     */
    public static <T> T getProperty(String propertyName, Class<T> valueType) {
        return UserInfoService.getProperty(propertyName, valueType);
    }

    /**
     * @return 用户全部信息
     */
    public static Map<String, ?> getUserInfo() {
        return UserInfoService.getUserInfo();
    }

    /**
     * 获取当前用户token值，需要spring security模块的支持。
     */
    public static String getTokenValue() {
        if (!TokenValueSupport.isSpringSecurity || (TokenValueSupport.tokenValueInited && TokenValueSupport.TokenValueField == null)) {
            //不是 spring security， 或者已经初始化但TokenValueField依然为null，说明不存在这样的token
            return null;
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.getDetails() != null) {
            Field tokenValueField = TokenValueSupport.getTokenValueField(authentication.getDetails());
            if (tokenValueField != null) {
                return (String) ReflectionUtils.getField(tokenValueField, authentication.getDetails());
            }
        }
        return null;
    }



}
